﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Directory2Rss
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using Data;

    /// <summary>
    /// Program runtime class
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Main program runtime method
        /// </summary>
        /// <param name="args">
        /// The arguments
        /// </param>
        public static void Main(string[] args)
        {
            // Load needed classes
            DataManager dataMan = new DataManager();
            FileSysManager fileSysManager = new FileSysManager();

            // Parse the command line arguments for the application
            SettingsManager.ParseArgs(args);

            // Iterate all the FolderGroups
            foreach (var folder in dataMan.FolderGroups)
            {
                // Scan the FileSystem and Load the list to memory
                List<Data.Types.Files> fileSysFiles = fileSysManager.GetFsFiles(folder.FolderGroupScanFolder);

                // Query the Database and Load the list to memory
                List<Data.Types.Files> databaseFiles = dataMan.GetDbFiles(folder.FolderGroupSk);

                // Compare the FileSystem and Database lists
                List<Data.Types.Files> filesToWrite = dataMan.GetNewFiles(fileSysFiles, databaseFiles);

                // If the comparison yields new files, write to database, else nothing
                if (filesToWrite.Count > 0)
                {
                    // Create the FileGroup for the entries
                    EntityKey fileGroupSkKey = dataMan.CreateFileGroup(folder.FolderGroupSk);

                    // Write new files to Database and return how many files were written
                    int newFileCounter = dataMan.WriteNewFiles(fileGroupSkKey, filesToWrite);

                    // Output results to console
                    Console.WriteLine(@"FolderGroup: {0} contained {1} new values", folder.FolderGroupName.PadRight(15, ' '), newFileCounter);
                }
                else
                {
                    // Output results to console
                    Console.WriteLine(@"FolderGroup: {0} contained no new values", folder.FolderGroupName.PadRight(15, ' '));
                }

                // Compare the FileSystem and Database to look for missing files
                List<Data.Types.Files> filesSkToDelete = dataMan.GetMissingFiles(fileSysFiles, databaseFiles);

                // If the comparison yields missing files, delete, else nothing
                if (filesSkToDelete.Count > 0)
                {
                    int deletedFileCounter = dataMan.DeleteMissingFiles(filesSkToDelete);

                    Console.WriteLine(@"FolderGroup: {0} contained {1} deleted values", folder.FolderGroupName.PadRight(15, ' '), deletedFileCounter);
                }

                // Create and set needed variables
                string outputFile = SettingsManager.RssOutputFolder + @"\" + folder.FolderGroupName.Replace(" ", "_") + ".xml";

                // Write the RSS XML File to the FileSystem
                fileSysManager.WriteRssXmlFile(outputFile, dataMan.GetRssXmlOutput(folder.FolderGroupSk));
            }

            // Exit the app silently if asked in command line
            if (SettingsManager.ArgIsSilent == false)
            {
                // Exit the program after reading a key
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
    }
}